package com.sinkdream.cms.controller;

import com.sinkdream.cms.model.FavoriteRelation;
import com.sinkdream.cms.model.News_one;
import com.sinkdream.cms.service.NewsService;
import com.sinkdream.cms.service.NewsService_one;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/SelectNews")
public class NewsSelectController {
    @Autowired
    private NewsService_one service;

    /**
     * 根据新闻类型，页数查询
     * @param
     * @return
     */
    @RequestMapping("/selectbt_type_page")
    @ResponseBody
    @CrossOrigin
    public List<News_one> selectbytype_page(@RequestParam("pagecount") String pagecount,
        @RequestParam("page") String page,@RequestParam("type_index") String type_index){
        List<News_one> list = null;
        HashMap<String,Object> map =new HashMap<String,Object>();
        map.put("pagecount",pagecount);//当前条数
        map.put("page",page);//查询的条数
        map.put("news_type",type_index);//查询的新闻类型
        list =service.selectbytype_page(map);
        return list;
    }
    /**
     * 搜索模糊查询
     * @param
     * @return
     */
    @RequestMapping("/selectbt_type_page_like")
    @ResponseBody
    @CrossOrigin
    public List<News_one> selectbt_type_page_like(@RequestParam("pagecount") String pagecount,
                                            @RequestParam("page") String page,@RequestParam("type_index") String type_index){
        List<News_one> list = null;
        HashMap<String,Object> map =new HashMap<String,Object>();
        map.put("pagecount",pagecount);//当前条数
        map.put("page",page);//查询的条数
        map.put("news_type",type_index);//查询的新闻关键字
        list =service.selectbytype_page_like(map);
        return list;
    }





    /**
     * 根据新闻标题id查询新闻内容
     */
    @RequestMapping("/selectbynews_id")
    @ResponseBody
    @CrossOrigin
    public List<News_one> selectbynews_id(@RequestParam("news_id") int news_id){
        List<News_one> list = null;
        list =service.selectbynews_id(news_id);
        return list;
    }


    /**
     * 根据用户id收藏新闻id
     */
    @RequestMapping("/userbynews_id")
    @ResponseBody
    @CrossOrigin
    public List<FavoriteRelation> selectbynews_id(FavoriteRelation relation){
        List<FavoriteRelation> list = null;
        list =service.selectshoucang(relation);
        return list;
    }

    /**
     * 根据点赞收藏信息 将数据插入到表中
     */
    @RequestMapping("/insertZ")
    @ResponseBody
    @CrossOrigin
    @Transactional
    public String insertZ(FavoriteRelation relation){
        List<FavoriteRelation> list = service.selectshoucang(relation);
        if(list.size()>0){
            service.updateByuserandnews(relation);//修改当前收藏或点赞信息
            service.updatezan(relation.getFavorite_news_id());//插入修改的新闻id和 1
        }else{
            service.insertZ(relation);//如果库不存在记录 就新插入。

            service.updatezan(relation.getFavorite_news_id());//插入修改的新闻id和 1
        }

        return "po";
    }


    /**
     * 根据点赞收藏信息 修改数据表
     */
    @RequestMapping("/updateZ")
    @ResponseBody
    @CrossOrigin
    @Transactional
    public String updateZ(FavoriteRelation relation){
        service.updateByuserandnews(relation);//修改当前收藏或点赞信息
        if(relation.getFavorite_is_zan()==1){ //如果之前收藏并未点赞，再次看这个新闻又点了赞
            service.updatezan(relation.getFavorite_news_id());//插入修改的新闻id和 1
        }

        return "po";
    }


    /**
     * 根据点赞收藏信息 修改数据表 取消点赞
     */
    @RequestMapping("/updateZdesc")
    @ResponseBody
    @CrossOrigin
    @Transactional
    public String updateZdesc(FavoriteRelation relation){
        service.updateByuserandnews(relation);//修改当前收藏或点赞信息
        if(relation.getFavorite_is_zan()==0){ //点了赞又取消点赞
            service.updatezandesc(relation.getFavorite_news_id());
        }

        return "pt";
    }
}
